package vrp;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

import java.io.IOException;
import javax.jdo.PersistenceManager;
import javax.servlet.http.*;

import vrp.PMF;
import vrp.Partner;

public class PartnerDeleteServlet extends HttpServlet {

    public void doPost(HttpServletRequest req, HttpServletResponse resp)
                throws IOException {
    	
    	PersistenceManager pm = PMF.get().getPersistenceManager();
    	
        long keyid = Integer.parseInt(req.getParameter("key"));
        
        Key key = KeyFactory.createKey(Partner.class.getSimpleName(), keyid);
        Partner partner = pm.getObjectById(Partner.class, key);
        
        int type = partner.getType();
        String responseRedirect = "";
        switch (type) {
        case 0:
        	responseRedirect += "/clients.jsp";
        	break;
        case 1:
        	responseRedirect += "/carriers.jsp";
        	break;
        case 2:
        	responseRedirect += "/whproviders.jsp";
        	break;
        }

        //check certain cases when cannot delete the partner
        
        
        try {
            pm.deletePersistent(partner);
        } finally {
            pm.close();
        }
        resp.sendRedirect(responseRedirect);
    }
}
